package com.yuejian.neusoft.yuejian_24.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuejian.neusoft.yuejian_24.R;
import com.yuejian.neusoft.yuejian_24.activity.ActivityDetailActivity;
import com.yuejian.neusoft.yuejian_24.activity.MarchDetailActivity;
import com.yuejian.neusoft.yuejian_24.activity.MasterDetailActivity;
import com.yuejian.neusoft.yuejian_24.activity.TrainDetailActivity;
import com.yuejian.neusoft.yuejian_24.javaBean.Search;

import java.util.List;

/**
 * Created by q4549 on 2017/11/02.
 */

public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    public SearchAdapter(Context mContext){
        this.mContext = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setList(List mDataList){
        this.mDataList = mDataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.search_cardview,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Search entity = (Search) mDataList.get(position);
        if (null == entity)
            return;
        final ViewHolder viewHolder = (ViewHolder) holder;
        if (entity.getAct_name()!=null) {
            viewHolder.serach_name.setText(entity.getAct_name());
            viewHolder.serach_type.setText("活动");
        }else if (entity.getMaster_name()!=null){
            viewHolder.serach_name.setText(entity.getMaster_name());
            viewHolder.serach_type.setText("教练");
        }else if (entity.getMarch_name()!=null){
            viewHolder.serach_name.setText(entity.getMarch_name());
            viewHolder.serach_type.setText("比赛");
        }else if (entity.getTrain_name()!=null){
            viewHolder.serach_name.setText(entity.getTrain_name());
            viewHolder.serach_type.setText("培训");
        }
        viewHolder.serach_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String type = viewHolder.serach_type.getText().toString();
                if (type.equals("活动")){
                    Intent intent = new Intent(mContext, ActivityDetailActivity.class);
                    intent.putExtra("act_id",entity.getAct_id());
                    mContext.startActivity(intent);
                }else if (type.equals("教练")){
                    Intent intent = new Intent(mContext, MasterDetailActivity.class);
                    intent.putExtra("mid",entity.getMaster_id());
                    mContext.startActivity(intent);
                }else if (type.equals("比赛")){
                    Intent intent = new Intent(mContext, MarchDetailActivity.class);
                    intent.putExtra("url",entity.getMarch_web());
                    intent.putExtra("march_id",entity.getMarch_id());
                    mContext.startActivity(intent);
                }else if (type.equals("培训")){
                    Intent intent = new Intent(mContext, TrainDetailActivity.class);
                    intent.putExtra("trainid",entity.getTrain_id());
                    mContext.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView serach_name,serach_type;
        LinearLayout serach_container;

        public ViewHolder(View itemView) {
            super(itemView);
            serach_name = (TextView) itemView.findViewById(R.id.search_name);
            serach_type = (TextView) itemView.findViewById(R.id.search_type);
            serach_container= (LinearLayout)itemView.findViewById(R.id.search_container);
        }
    }
}
